package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketGoodsProductMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.model.MarketGoodsExample;
import com.cskaoyan.model.MarketGoodsProductExample;
import com.cskaoyan.model.MarketOrderExample;
import com.cskaoyan.model.MarketUserExample;
import com.cskaoyan.service.DashboardService;
import com.cskaoyan.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;

public class DashboardServiceImpl implements DashboardService {
    @Override
    public HashMap<String, Object> countTotal() {
        HashMap<String, Object> dataMap = new HashMap<>();
        SqlSession sqlsession = MyBatisUtil.getSqlSession();

        //1.goodsTotal
        MarketGoodsMapper marketGoodsMapper = sqlsession.getMapper(MarketGoodsMapper.class);
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        marketGoodsExample.createCriteria().andDeletedEqualTo(false);
        int goodsTotal = (int) marketGoodsMapper.countByExample(marketGoodsExample);
        dataMap.put("goodsTotal", goodsTotal);

        //2.userTotal
        MarketUserMapper marketUserMapper = sqlsession.getMapper(MarketUserMapper.class);
        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.createCriteria().andDeletedEqualTo(false);
        int userTotal = (int) marketUserMapper.countByExample(marketUserExample);
        dataMap.put("userTotal", userTotal);

        //3.productTotal
        MarketGoodsProductMapper marketGoodsProductMapper = sqlsession.getMapper(MarketGoodsProductMapper.class);
        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        marketGoodsExample.createCriteria().andDeletedEqualTo(false);
        int productTotal = (int) marketGoodsProductMapper.countByExample(marketGoodsProductExample);
        dataMap.put("productTotal", productTotal);

        //4.orderTotal
        MarketOrderMapper marketOrderMapper = sqlsession.getMapper(MarketOrderMapper.class);
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.createCriteria().andDeletedEqualTo(false);
        int orderTotal = (int) marketOrderMapper.countByExample(marketOrderExample);
        dataMap.put("orderTotal", orderTotal);

        sqlsession.commit();
        sqlsession.close();
        return dataMap;
    }
}
